package com.zhangwei.yueshijia.presenter;

import com.zhangwei.yueshijia.api.BaseSubscriber;
import com.zhangwei.yueshijia.api.ExceptionHandle;
import com.zhangwei.yueshijia.base.BasePresenter;
import com.zhangwei.yueshijia.bean.BaseBean;
import com.zhangwei.yueshijia.bean.home.HomeData;
import com.zhangwei.yueshijia.model.HomeModel;
import com.zhangwei.yueshijia.view.impl.HomeView;

import java.util.Map;

public class HomePresenter extends BasePresenter<HomeView> {
    HomeModel model;

    public HomePresenter() {
        this.model = new HomeModel();
    }

    public void getHomeData(Map<String, String> params) {
        view.onShow();
        model.getHomeData(params, new BaseSubscriber<BaseBean<HomeData>>() {
            @Override
            public void onSuccess(BaseBean<HomeData> data) {
                if (data != null && data.getDatas() != null) {
                    if (data.getDatas().getBanner() != null && !data.getDatas().getBanner().isEmpty()) {
                        view.onSuccessHeadData(data.getDatas().getBanner());
                    }
                    if (data.getDatas().getDataType() != null) {
                        view.onSuccess(data.getDatas().getDataType(), data.isHasmore(), data.getPageTotal());
                    }
                } else {
                    view.onFial(new ExceptionHandle.ResponeThrowable("数据为空", ExceptionHandle.ERROR.DATA_EMPTY));
                }
            }

            @Override
            public void onFail(ExceptionHandle.ResponeThrowable e) {
                view.onFial(e);
            }

            @Override
            public void onFinish() {
                view.onFinish();
            }
        });
    }

}
